package com.application.fabric.test.Token;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;

public class Token02 {
    //密钥
    public static final String SUBJECT = "API-TOKEN-LJH";
    //key
    public static final String APPSECRET = "API-TOKEN-LJH";
    public static final long EXPIRE = 1000 * 60 * 60; //过期时间，毫秒，60分钟


    /**
     * @Author: ljh
     * @Description: jwt生成token
     * @DateTime: 11:50 2022/11/2
     * @Params: name一般是登录账户名  id是账户id或者随机数值 ip请求登录着的IP地址
     * @Return
     */
    public  String geneJsonWebToken(String key, String Phone) {
        String token = Jwts.builder().setSubject(SUBJECT)
                .claim("userKey", key)
                .claim("userPhone", Phone)
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis() + EXPIRE))
                .signWith(SignatureAlgorithm.HS256, APPSECRET).compact();
        return token;
    }


    /**
     * @Author: ljh
     * @Description: 校验token
     * @DateTime: 11:50 2022/11/2
     * @Params:
     * @Return
     */
    public  Claims checkJWT(String token) {
        Claims claims = Jwts.parser().setSigningKey(APPSECRET).parseClaimsJws(token).getBody();
        return claims;

    }
}
